package com.wwtech.epp.ui.activity.work;

import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.ui.activity.work.component.DaggerWebViewComponent;
import com.wwtech.epp.ui.activity.work.contract.WebViewContract;
import com.wwtech.epp.ui.activity.work.module.WebViewModule;
import com.wwtech.epp.ui.activity.work.presenter.WebViewPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/10/16 14:25:40
 */

public class WebViewActivity extends BaseActivity implements WebViewContract.View {

    @Inject
    WebViewPresenter mPresenter;
    @BindView(R.id.webView)
    WebView webView;
    String url = "";

    private final String MicrosoftOnlinePreviewUrl = "https://view.officeapps.live.com/op/view.aspx?src=";   //https://view.officeapps.live.com/op/view.aspx?src=

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_web_view);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle(getIntent().getStringExtra("title"));
    }

    @Override
    protected void initData() {
        //http://www.vv-tech.com/static/help.html
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDefaultFontSize(16);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setDefaultTextEncodingName("UTF -8");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        url = getIntent().getStringExtra("url");
        if (getIntent().getStringExtra("title").equals("附件详情")) {
            webView.getSettings().setTextZoom(500);
        }
        webView.getSettings().setLayoutAlgorithm(
                WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        if (getIntent().getStringExtra("title").equals("公告详情")) {
            webView.loadUrl(url);
        } else {
            if (isMicrosoftFile(url)) {
                webView.getSettings().setTextZoom(100);
                webView.loadUrl(MicrosoftOnlinePreviewUrl + url);
                KLog.i(webView.getUrl());
            } else {
//             WebView加载web资源
                webView.loadUrl(url);
            }
        }
        KLog.i(url);
//        webView.loadUrl("http://www.baidu.com");
        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                KLog.i("作为客户端连接网址" + url);
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                if (getIntent().getStringExtra("title").equals("公告详情")) {
                    if (isMicrosoftFile(url)) {
                        if (url.contains("officeapps")) {
                            view.loadUrl(url);
                        } else {
                            view.loadUrl(MicrosoftOnlinePreviewUrl + url);
                        }
                        KLog.i(view.getUrl());
                    } else {
                        view.loadUrl(url);
                    }
                } else {
                    view.loadUrl(url);
                }

                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

        });


        webView.getSettings().setCacheMode(
                WebSettings.LOAD_NO_CACHE);
    }

    private boolean isMicrosoftFile(String url) {
        if (url.contains("doc") || url.contains("xls") || url.contains("ppt")) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void setupActivityComponent() {
        DaggerWebViewComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .webViewModule(new WebViewModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(WebViewContract.WebViewContractPresenter presenter) {
        mPresenter = (WebViewPresenter) presenter;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isMicrosoftFile(url)) {
            finish();
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.getUrl().contains("officeapps")) {
                webView.loadUrl(url);// 返回上一页面
                return true;
            } else {
                finish();// 退出程序
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

}